package simpleservletcontainer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Properties;

public class HttpServer {
	/**
	 * WEB_ROOT
	 * SHUTDOWN
	 * CONF
	 * port 
	 * host
	 * SHUTDOWN
	 */
	
	public static final String WEB_ROOT = System.getProperty("user.dir")+File.separator+"ROOT";
	public static final String SERVLET_ROOT = System.getProperty("user.dir")+File.separator+"bin/";
	
	private static final String SHUTDOWN_CMD = "/SHUTDOWN";
	private static final String SERVLET_CMD = "/servlet";
	private static final String CONF = System.getProperty("user.dir")+File.separator+"conf/server.conf";
	private static int port = 8080;
	private static String host = "127.0.0.1";
	private boolean isShutdown = false;
	
	static {
		Properties info = new Properties();
		try {
			InputStream in = new FileInputStream(CONF);
			info.load(in);
			if(info.getProperty("port") != null) {
				port = Integer.parseInt(info.getProperty("port"));
			}
			if(info.getProperty("host") != null) {		
				host = info.getProperty("host");
			}
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		}
	}
	
	public void await() {
		// 初始化服务器
		ServerSocket serverSocket=null;
		try {
			System.out.println(String.format("init server %s:%d", host, port));
			serverSocket = new ServerSocket(port, 1, InetAddress.getByName(host));
			System.out.println("inited server");
		} catch (IOException e) {
			System.exit(1);
			e.printStackTrace();
		}
		// 循环等待连接
		while(!isShutdown) {
			Socket socket;
			InputStream input;
			OutputStream output;
			try {
				socket = serverSocket.accept();
				System.out.println("connected form "+socket.getInetAddress());
				input = socket.getInputStream();
				output = socket.getOutputStream();
				// 请求对象
				Request request = new Request(input);
				request.parse();
				// 响应对象
				Response response = new Response(output);
				response.setRequest(request);
				
				// 判断并响应servlet或者静态资源
				if(request.getUri().startsWith(SERVLET_CMD)) {
					System.out.println(request.getUri());
					ServletProcessor servletProcessor = new ServletProcessor();
					System.out.println("servletProcessor");
					try {
						System.out.println("process");
						servletProcessor.process(request, response);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}else {
					System.out.println("staticResourceProcessor");
					StaticResourceProcessor staticResourceProcessor = new StaticResourceProcessor();
					staticResourceProcessor.process(request, response);
				}
				System.out.println("connect end");
				socket.close();
				isShutdown = request.getUri().equals(SHUTDOWN_CMD);
				System.out.println("connect end");
			} catch (IOException e) {
				System.exit(1);
				e.printStackTrace();
			}
		}
	}	
	public static void main(String[] a) {
		HttpServer server = new HttpServer();
		server.await();
	}
}
